package org.jivesoftware.smackx.ping;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.ping.packet.Ping;
import org.jivesoftware.smackx.ping.packet.Pong;

public class PingManager
{
  public static final String NAMESPACE = "urn:xmpp:ping";
  public static final String ELEMENT = "ping";
  private static final Map<Connection, PingManager> instances = Collections.synchronizedMap(new WeakHashMap());
  private static final ScheduledExecutorService periodicPingExecutorService = new ScheduledThreadPoolExecutor(1);
  private Connection connection;
  private int pingInterval = SmackConfiguration.getDefaultPingInterval();
  private Set<PingFailedListener> pingFailedListeners = Collections.synchronizedSet(new HashSet());
  private ScheduledFuture<?> periodicPingTask;
  protected volatile long lastSuccessfulPingByTask = -1L;
  private long pingMinDelta = 100L;
  private long lastPingStamp = 0L;
  private long lastSuccessfulManualPing = -1L;

  static
  {
    Connection.addConnectionCreationListener(new ConnectionCreationListener()
    {
      public void connectionCreated(Connection paramAnonymousConnection)
      {
        PingManager.getInstanceFor(paramAnonymousConnection);
      }
    });
  }

  private PingManager(final Connection paramConnection)
  {
    this.connection = paramConnection;
    instances.put(paramConnection, this);
    ServiceDiscoveryManager localServiceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(paramConnection);
    localServiceDiscoveryManager.addFeature("urn:xmpp:ping");
    PacketTypeFilter localPacketTypeFilter = new PacketTypeFilter(Ping.class);
    paramConnection.addPacketListener(new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        if (PingManager.this.pingMinDelta > 0L)
        {
          long l1 = System.currentTimeMillis();
          long l2 = l1 - PingManager.this.lastPingStamp;
          PingManager.this.lastPingStamp = l1;
          if (l2 < PingManager.this.pingMinDelta)
            return;
        }
        Pong localPong = new Pong((Ping)paramAnonymousPacket);
        paramConnection.sendPacket(localPong);
      }
    }
    , localPacketTypeFilter);
    paramConnection.addConnectionListener(new ConnectionListener()
    {
      public void connectionClosed()
      {
        PingManager.this.maybeStopPingServerTask();
      }

      public void connectionClosedOnError(Exception paramAnonymousException)
      {
        PingManager.this.maybeStopPingServerTask();
      }

      public void reconnectionSuccessful()
      {
        PingManager.this.maybeSchedulePingServerTask();
      }

      public void reconnectingIn(int paramAnonymousInt)
      {
      }

      public void reconnectionFailed(Exception paramAnonymousException)
      {
      }
    });
    maybeSchedulePingServerTask();
  }

  public static synchronized PingManager getInstanceFor(Connection paramConnection)
  {
    PingManager localPingManager = (PingManager)instances.get(paramConnection);
    if (localPingManager == null)
      localPingManager = new PingManager(paramConnection);
    return localPingManager;
  }

  public void setPingIntervall(int paramInt)
  {
    this.pingInterval = paramInt;
  }

  public int getPingIntervall()
  {
    return this.pingInterval;
  }

  public void registerPingFailedListener(PingFailedListener paramPingFailedListener)
  {
    this.pingFailedListeners.add(paramPingFailedListener);
  }

  public void unregisterPingFailedListener(PingFailedListener paramPingFailedListener)
  {
    this.pingFailedListeners.remove(paramPingFailedListener);
  }

  public void disablePingFloodProtection()
  {
    setPingMinimumInterval(-1L);
  }

  public void setPingMinimumInterval(long paramLong)
  {
    this.pingMinDelta = paramLong;
  }

  public long getPingMinimumInterval()
  {
    return this.pingMinDelta;
  }

  public IQ ping(String paramString, long paramLong)
  {
    if (!this.connection.isAuthenticated())
      return null;
    Ping localPing = new Ping(this.connection.getUser(), paramString);
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localPing.getPacketID()));
    this.connection.sendPacket(localPing);
    IQ localIQ = (IQ)localPacketCollector.nextResult(paramLong);
    localPacketCollector.cancel();
    return localIQ;
  }

  public IQ ping(String paramString)
  {
    return ping(paramString, SmackConfiguration.getPacketReplyTimeout());
  }

  public boolean pingEntity(String paramString, long paramLong)
  {
    IQ localIQ = ping(paramString, paramLong);
    if ((localIQ == null) || (localIQ.getType() == IQ.Type.ERROR))
      return false;
    pongReceived();
    return true;
  }

  public boolean pingEntity(String paramString)
  {
    return pingEntity(paramString, SmackConfiguration.getPacketReplyTimeout());
  }

  public boolean pingMyServer(long paramLong)
  {
    IQ localIQ = ping(this.connection.getServiceName(), paramLong);
    if (localIQ == null)
    {
      Iterator localIterator = this.pingFailedListeners.iterator();
      while (localIterator.hasNext())
      {
        PingFailedListener localPingFailedListener = (PingFailedListener)localIterator.next();
        localPingFailedListener.pingFailed();
      }
      return false;
    }
    pongReceived();
    return true;
  }

  public boolean pingMyServer()
  {
    return pingMyServer(SmackConfiguration.getPacketReplyTimeout());
  }

  public boolean isPingSupported(String paramString)
  {
    try
    {
      DiscoverInfo localDiscoverInfo = ServiceDiscoveryManager.getInstanceFor(this.connection).discoverInfo(paramString);
      return localDiscoverInfo.containsFeature("urn:xmpp:ping");
    }
    catch (XMPPException localXMPPException)
    {
    }
    return false;
  }

  public long getLastSuccessfulPing()
  {
    return Math.max(this.lastSuccessfulPingByTask, this.lastSuccessfulManualPing);
  }

  protected Set<PingFailedListener> getPingFailedListeners()
  {
    return this.pingFailedListeners;
  }

  protected synchronized void maybeSchedulePingServerTask()
  {
    maybeStopPingServerTask();
    if (this.pingInterval > 0)
      this.periodicPingTask = periodicPingExecutorService.schedule(new ServerPingTask(this.connection), this.pingInterval, TimeUnit.SECONDS);
  }

  private void maybeStopPingServerTask()
  {
    if (this.periodicPingTask != null)
    {
      this.periodicPingTask.cancel(true);
      this.periodicPingTask = null;
    }
  }

  private void pongReceived()
  {
    this.lastSuccessfulManualPing = System.currentTimeMillis();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.ping.PingManager
 * JD-Core Version:    0.6.1
 */